﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ALiteTeam.PackageTracer.Service
{
    public class SfFilter : BaseFilter
    {
        private static readonly string REGX = "<td align=\"center\" height=\"10\" bgcolor=\"#e2e2e2\">(?<content>.*)</td>";
        private static readonly string REGX_PART = @"<font color='red'>(?<addr>[^<>]+)</font>";

        private Regex _Regex;
        private MatchCollection _Col;
        private bool _IsSuessed;

        private void Init()
        {
            if (_Regex == null)
            {
                _Regex = new Regex(REGX, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                _Col = _Regex.Matches(_Content);
            }

            if (_Col.Count > 0)
            {
                _IsSuessed = true;
            }
        }

        //获取当前状态
        public override QueryResult GetSummary(string content)
        {
            QueryResult result;
            
            _Content = content;            
            this.Init();

            if (_IsSuessed == true)
            {
                var date = _Col[1].Groups["content"].Value;
                var time = _Col[2].Groups["content"].Value;
                var message = _Col[3].Groups["content"].Value;
                var city = _Col[4].Groups["content"].Value;

                result = new QueryResult()
                {
                    OrderNo = _OrderNo,
                    Status = 1,
                    Summary = "ok",
                    Details = new List<QueryResult.Detail>
                    {
                        new QueryResult.Detail()
                        {
                             FTime = DateTime.Parse(string.Format("{0} {1}", date, time)), 
                             Context = string.Format("{0}: {1}", city, message)
                        }
                    }
                };
            }
            else
            {
                result = new QueryResult()
               {
                   OrderNo = _OrderNo,
                   Status = 0,
                   Summary = "查询失败，可能是顺丰官网服务中止"
               };
            }

            return result;
        }

        public override QueryResult GetDetails(string content)
        {
            QueryResult result;

            _Content = content;
            this.Init();

            if (_IsSuessed == true)
            {
                result = new QueryResult()
                {
                    OrderNo = _OrderNo,
                    Status = 1,
                    Summary = "ok",
                    Details = new List<QueryResult.Detail>()
                };

                string date;
                string time;
                string desc = string.Empty;
                string city;

                for (var i = 6; i < _Col.Count - 3; i += 4)
                {
                    date = _Col[i].Groups["content"].Value;
                    time = _Col[i + 1].Groups["content"].Value;
                    city = _Col[i + 3].Groups["content"].Value;

                    // 稍复杂的待处理字符串
                    var unhandledComplex = _Col[i + 2].Groups["content"].Value;

                    Regex regx = new Regex(REGX_PART, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    var colDesc = regx.Matches(unhandledComplex);

                    if (colDesc.Count == 2)
                    {
                        var from = colDesc[0].Groups["addr"].Value.Trim();
                        var destination = colDesc[1].Groups["addr"].Value.Trim();

                        desc = string.Format("{0}-->{1}", from, destination);
                    }
                    else
                    {
                        if (unhandledComplex.IndexOf("<") < 0)
                        {
                            desc = unhandledComplex;
                        }
                    }

                    var item = new QueryResult.Detail()
                    {
                        FTime = DateTime.Parse(string.Format("{0} {1}", date, time)),
                        Context = string.Format("{0}: {1}", city, desc)
                    };

                    result.Details.Add(item);
                }
            }
            else
            {
                result = new QueryResult()
                {
                    OrderNo = _OrderNo,
                    Status = 0,
                    Summary = "查询失败，可能是顺丰官网服务中止"
                };
            }

            return result;
        }
    }
}
